#include <stdio.h>
#include <string.h>
#include "mpi.h"

#define N 100

int main (int argc, char** argv) 
{
    int my_rank; /* Rank of process */
    int p; /* Number of processes */
    int source; /* Rank of sender */
    int dest; /* Rank of receiver */
    int tag = 50; /* Tag for messages */
    char message[100]; /* Storage for the message */
    MPI_Status status; /* Return status for receive */

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
    MPI_Comm_size(MPI_COMM_WORLD, &p);

    int n = N / p;
    float sum = 0.0;
    float total_sum;
    int i;
    for (i = my_rank * n; i < (my_rank + 1) * n; i++)
	sum += i + 1;
    // 全部加到 0 号 CPU中，一个结果。
    MPI_Reduce(&sum, &total_sum, 1, MPI_FLOAT, MPI_SUM, 0, MPI_COMM_WORLD);

    if (my_rank == 0) 
    { /* my_rank == 0 */
	for (i = n * p; i < N; ++i)
	    total_sum += i + 1;
	printf("n = %d\n", n);
	printf("%f\n", total_sum);
    }
    
    MPI_Finalize();
    return 0;
} /* main */

/* void Get_data2(int my_rank, float* a_ptr, float* b_ptr, int* n_ptr)  */
/* { */
/*     int root = 0; /\* Arguments to MPI_Bcast *\/ */
/*     int count = 1; */
/*     if (my_rank == 0) */
/*     { */
/* 	printf("Enter a, b, and n\n"); */
/* 	scanf("%f %f %d", a_ptr, b_ptr, n_ptr); */
/*     } */
/*     MPI_Bcast(a_ptr, 1, MPI_FLOAT, root, */
/* 	      MPI_COMM_WORLD); */
/*     MPI_Bcast(b_ptr, 1, MPI_FLOAT, root, */
/* 	      MPI_COMM_WORLD); */
/*     MPI_Bcast(n_ptr, 1, MPI_INT, root, */
/* 	      MPI_COMM_WORLD); */
/* } /\* Get_data2 *\/ */
